'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en'], function ($) {
  return {
    
    init: function () {

      this.confirmed = false;
      this.initPage();
      this.initCreate();
      this.initDelete();
      this.resetPassword();
      
    },

    initPage: function () {
      
      var that = this;
      
      $(document).on("change", 'input[name="loginName"]', function(e){
        that.confirmed = false;
      })

    },
    
    initCreate: function () {
      
      var that = this;
      
      $(document).on("shown.bs.modal", "#create", function (e) {

        $(document).on('change', 'select[name="orgId"]', function(el) {
          var selectOrg = $('select[name="orgId"] option:selected').val();
          if (selectOrg == 1000 || selectOrg == 1001 || selectOrg == 1002 || selectOrg == 1127 || selectOrg == 3230 || selectOrg == 3191) {
            
            // 展示VAM需要选择框
            $('.vam-user').show();
            $('select[name="departmentId"]').data('validate', 'required');
            
            // 只显示选择公司的部门
            $('select[name="departmentId"] option').each(function() {
              var org = $(this).data('org');
              if (selectOrg == org) {
                $(this).prop('disabled', false).removeClass('hide');
              } else {
                $(this).prop('disabled', true).addClass('hide');
              }
            });
            
          } else {
            
            $('.vam-user').hide();
            $('select[name="departmentId"]').data('validate', '');
            
          }
        });
        
        $(document).on('change', 'select[name="departmentId"]', function(el) {
          var departmentName = $('select[name="departmentId"] option:selected').text();
          $('input[name="departmentName"]').val(departmentName);
        });
        
        
        // 提交
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

        var $form = $(e.currentTarget).find("form")              //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            
            $form.trigger("validate.ajax.submit");
            
            var result = $.bt_validate.result[$.bt_validate.form_id]
            if (result) {
              return that.confirmed ? true : that.validateLoginName();
            } else {
              return false;
            }
            
          }
        })
        
      });
      
    },
    
    validateLoginName: function() {
      
      var that = this;
      
      var loginName = $('input[name="loginName"]').val().trim();
      if (loginName === "" || loginName === undefined) {
        return false;
      }
      
      $.ajax({
        url: __CTX__.root + "/admin-users/exist-login-name",
        data: {
          loginName: loginName
        }
      }).done(function(res){
      
        res = typeof res === 'String' ? JSON.parse(res) : res;
        if (res.data.existLoginName) {
          $("input[name='loginName']").show_err_tooltip('该登录名已存在!');
          return false;
        }
        
        that.confirmed = true;
        $("#create form button[type='submit']").click();
      
      }).fail(function(){
        return false;
      });
      
    },
    
    initDelete: function() {
      
      $(document).on("shown.bs.modal", "#delete", function (e) {

        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

        var $form = $(e.currentTarget).find("form")              //bind shiftform
        
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })

      });
      
    },
    
    resetPassword: function() {
      
      $(document).on("shown.bs.modal", "#reset", function (e) {
      
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
  
        var $form = $(e.currentTarget).find("form")              //bind shiftform
        
        $form.shiftform({
          beforeSubmit: function (el) {
            
            var password = $('input[name="password"]').val().trim();
            var confirmPassword = $('input[name="confirmPassword"]').val().trim();
            
            if (!(password === confirmPassword)) {
              $('input[name="confirmPassword"]').show_err_tooltip('两次密码不一致!');
              return false;
            }
            
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
            
          }
        })
  
      });
    }
    
  }
})

